﻿Imports DTO
Imports BUS

Public Class frmLapDanhSachLop

    Private Sub LoadLopHoc()
        Dim lhBus As New LopHocBus()
        Dim danhSachLop As List(Of LopHocDto)
        danhSachLop = lhBus.LayDSLop()
        cbbLopHoc.DataSource = danhSachLop
        cbbLopHoc.ValueMember = "MaLop"
        cbbLopHoc.DisplayMember = "TenLop"
    End Sub

    Private Sub frmLapDanhSachLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbGioiTinh.SelectedItem = cbbGioiTinh.Items(0)
        LoadLopHoc()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub LapDanhSachLop()
        Dim hsDTO As New HocSinhDto()
        Dim lh As LopHocDto = cbbLopHoc.SelectedItem
        Dim maLop As Integer = lh.MaLop
        For i As Integer = 0 To dgvDanhSachLop.Rows.Count - 2
            hsDTO.TenHS = dgvDanhSachLop.Rows(i).Cells(1).Value
            hsDTO.GioiTinh = dgvDanhSachLop.Rows(i).Cells(2).Value
            hsDTO.NgaySinh = dgvDanhSachLop.Rows(i).Cells(3).Value
            hsDTO.DiaChi = dgvDanhSachLop.Rows(i).Cells(4).Value
            HocSinhBus.LapDanhSachLop(hsDTO, maLop)
        Next
    End Sub

    Private Sub btnLapDSLop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapDSLop.Click
        LapDanhSachLop()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click

        Dim n As Integer = dgvDanhSachLop.Rows.Add()
        dgvDanhSachLop.Rows.Item(n).Cells("clSTT").Value = (dgvDanhSachLop.Rows.Count - 1).ToString()
        dgvDanhSachLop.Rows.Item(n).Cells("clHoTen").Value = txtHoTen.Text
        dgvDanhSachLop.Rows.Item(n).Cells("clGioiTinh").Value = cbbGioiTinh.SelectedItem.ToString()
        dgvDanhSachLop.Rows.Item(n).Cells("clNamSinh").Value = dtpNamSinh.Value
        dgvDanhSachLop.Rows.Item(n).Cells("clDiaChi").Value = txtDiaChi.Text
    End Sub

    Private Sub dgvDanhSachLop_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachLop.SelectionChanged
        If dgvDanhSachLop.SelectedRows.Count = 0 Then
            Return
        End If
        Dim index As Integer = dgvDanhSachLop.CurrentRow.Index
        If index >= dgvDanhSachLop.Rows.Count - 1 Then
            Return
        End If
        Dim row As DataGridViewRow = dgvDanhSachLop.Rows(index)
        txtHoTen.Text = row.Cells("clHoTen").Value
        txtDiaChi.Text = row.Cells("clDiaChi").Value
        dtpNamSinh.Value = row.Cells("clNamSinh").Value
        cbbGioiTinh.Text = row.Cells("clGioiTinh").Value
    End Sub

    Sub Clear()
        txtHoTen.Text = ""
        txtDiaChi.Text = ""
        cbbGioiTinh.SelectedIndex = 0
        dtpNamSinh.Value = Date.Now.ToString()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If dgvDanhSachLop.SelectedRows.Count = 0 Then
            MessageBox.Show("Chua Chon Dai Ly De Xoa")
            Return
        End If
        Dim index As Integer = dgvDanhSachLop.CurrentRow.Index
        If MessageBox.Show("ban co thuc su muon xoa hay khong", "thong bao", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            dgvDanhSachLop.Rows.RemoveAt(index)
            Clear()
        End If
    End Sub
End Class